package org.jetlinks.community.mstn.handler;

import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.core.param.Sort;
import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.Authentication;
import org.jetlinks.community.mstn.MstnAssetsHolder;
import org.jetlinks.community.mstn.logging.MstnAssetsRequestCtx;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class MstnAssetsQueryParamHandler implements MstnAssetsHandler{

    private MstnAssetsDefine ann;
    private boolean returnMono;
    private boolean returnFlux;

    public MstnAssetsQueryParamHandler(MstnAssetsDefine ann, boolean returnMono, boolean returnFlux) {

        this.ann = ann;
        this.returnMono = returnMono;
        this.returnFlux = returnFlux;
    }

    @Override
    public Mono<Void> beforeHandler(MstnAssetsRequestCtx requestCtx) {
        return Authentication.currentReactive().flatMap(authentication -> {
            MstnAssetsHolder holder = MstnAssetsHolder.check(authentication, requestCtx, ann.type);
            if (holder != null) {
                requestCtx.setTerm(holder.injectQuery());
            }
            return Mono.empty();
        });
    }
}
